// pages/case/detaile/index.js
const app = getApp()
import api from '../../../api/main.js'
import tools from '../../../utils/tools.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    data: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    //获取案件ID
    var id = 0
    if (options.id) {
      that.data.id = options.id
    }
    that.loadData()
  },
  /**
   * 加载数据
   */
  loadData() {
    var that = this
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    api.cases.getCase({
      id: that.data.id
    }).then(res => {
      wx.hideLoading()
      if (res.code == app.globalData.successCode && res.success) {
        console.log(res.data)
        var data = res.data
        //加入民生类型名称
        if (data.peopleLivehoodId && data.peopleLivehoodId > 0) {
          for (var j = 0; j < app.globalData.peopleLivehoodList.length; j++) {
            const peopleLivehood = app.globalData.peopleLivehoodList[j]
            if (peopleLivehood.id == data.peopleLivehoodId) {
              data["peopleLivehoodName"] = peopleLivehood.name
            }
          }
        } else {
          data["peopleLivehoodName"] = ''
        }
        //更新时间转换
        if (data.currentCaseTime) {
          data['currentCaseTime'] = tools.stringToDate(data.currentCaseTime, 'Y-M-D h:m:s')
        } else {
          data['currentCaseTime'] = ''
        }
        //用户是否是法官
        if (app.globalData.userType == '1') {
          data['isJudge'] = true
        } else {
          data['isJudge'] = false
        }
        var caseStages = []
        for (var i = 0; i < data.caseStages.length; i++) {
          var item = data.caseStages[i]
          //更换法官权限
          if(app.globalData.userType == '1'){
            item['chengJudge'] = true
          }else{
            item['chengJudge'] = true
          }
          //完成时间转换
          if(item.finishTime){
            item['finishTime'] = tools.stringToDate(item.finishTime, 'Y-M-D h:m:s')
          }else{
            item['finishTime'] = ''
          }
          
          //状态转为中文
          var statusStr = '未开始'
          switch (item.status) {
            case 1:
              statusStr = "执行中"
              break
            case 2:
              statusStr = "已完成"
              break
            case 3:
              statusStr = "已完成"
              break
            case -1:
              statusStr = "已撤回"
              break
          }
          item['statusStr'] = statusStr
          caseStages.push(item)
        }
        data.caseStages = caseStages
        that.setData({
          data: data
        })
        caseStages = null
        data = null
      }
      res = null

    })
  },
  /**
   * 案件操作事件
   * @param index 0 更换法官 1 设置辅助法官 2 申请撤回 3 阶段完结
   */
  onCaseChange(e){
    const index = parseInt(e.target.dataset.index)
    console.log(index)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})